//
//  CAMineViewController.m
//  LNH
//
//  Created by twentylx on 2018/1/15.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "CAMineViewController.h"
#import "CAMineHeaderView.h"
#import "CAMoneyViewController.h"
#import "CAMyQRCodeViewController.h"
#import "CAHBXXViewController.h"
#import "CAHBJLViewController.h"
#import "CAMembersSearchViewController.h"
#import "CAMembersViewController.h"
#import "CAAddressViewController.h"
#import "CALoginViewController.h"
#import "CABankCardViewController.h"
#import "CAUserInfomationApi.h"
#import "CAOrderListViewController.h"
#import "CAReturnGoodsListViewController.h"

@interface CAMineViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *mineTableView;
@property (nonatomic, strong) CAMineHeaderView *headView;
@property (nonatomic, strong) NSArray *dataArray;

@end

static NSString *cellIndentifier = @"mineIdentifier";

@implementation CAMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self getInfomation];
}

- (void)lm_initialDefaultsForController
{
    NSArray *stepOneArray = @[
                             @{@"text":@"我的余额",@"image":@"yue"},
                             @{@"text":@"我的二维码",@"image":@"ewm"},
                             @{@"text":@"红包奖励消息",@"image":@"hbjlxx"},
                             @{@"text":@"红包奖励列表",@"image":@"hbjllb"},
                             @{@"text":@"成员查询",@"image":@"cycx"},
                             @{@"text":@"成员列表",@"image":@"cylb"},
                             ];
    NSDictionary *stepOne = @{@"rowData" : stepOneArray};
    
    NSArray *stepTwoArray = @[
                              @{@"text":@"QQ客服电话",@"image":@"kfdh"},
                              @{@"text":@"修改地址",@"image":@"xgdz"},
                              @{@"text":@"我的银行卡",@"image":@"yhk"},
                              @{@"text":@"帮助与反馈",@"image":@"bzyfk"},
                              @{@"text":@"诚信经营",@"image":@"cxjy"},
                              @{@"text":@"退出登录",@"image":@"tcdl"},
                              ];
    NSDictionary *stepTwo = @{@"rowData" : stepTwoArray};
    
    
    self.dataArray = @[stepOne, stepTwo];
}

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0
- (UIRectEdge)edgesForExtendedLayout {
    return UIRectEdgeNone;
}
#endif

- (void)lm_configNavigationForController
{
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.tintColor = RGBCOLOR(113, 113, 113);
    self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : RGBCOLOR(35, 24, 21), NSFontAttributeName : [UIFont systemFontOfSize:16]};
    
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)lm_createViewForController
{
    [self.view addSubview:self.mineTableView];
    
    [self.view setNeedsUpdateConstraints];
}

-(void)lm_bindViewModelForController
{
    
}

#pragma mark layout
- (void) updateViewConstraints {
    [super updateViewConstraints];
    
    [_mineTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

#pragma mark get & set
- (CAMineHeaderView *) headView {
    if (!_headView) {
        if (isIPhoneX) {
            _headView = [[CAMineHeaderView alloc] initWithFrame:(CGRect){ 0 ,0 ,SCREEN_WIDTH, 336}];
        } else {
            _headView = [[CAMineHeaderView alloc] initWithFrame:(CGRect){ 0 ,0 ,SCREEN_WIDTH, 316}];
        }
        _headView.backgroundColor = [UIColor clearColor];
        NSLog(@"%@", GVUD.portrait);
        if (GVUD.isLogin) {
            [_headView.logoImageView sd_setImageWithURL:[NSURL URLWithString:GVUD.portrait]];
            _headView.nameLabel.text = [GVUserDefaults standardUserDefaults].name;
        }
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] init];
        [_headView.logoImageView addGestureRecognizer:gesture];

        [[gesture rac_gestureSignal] subscribeNext:^(id x) {
             if (!GVUD.isLogin) {
                CALoginViewController *loginVC = [CALoginViewController new];
                loginVC.hidesBottomBarWhenPushed = YES;
    //            loginVC.infomationModel = _informationModel;
                [self.navigationController pushViewController:loginVC animated:YES];
             }
        }];
        UITapGestureRecognizer *orderGesture = [[UITapGestureRecognizer alloc] init];
        [_headView.myOrderView addGestureRecognizer:orderGesture];
        [[orderGesture rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
            CAOrderListViewController *orderListVC = [CAOrderListViewController new];
            orderListVC.status = @"";
            orderListVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:orderListVC animated:YES];
        }];
        //待付款
        [[_headView.payBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            CAOrderListViewController *orderListVC = [CAOrderListViewController new];
            orderListVC.status = @"1";
            orderListVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:orderListVC animated:YES];
        }];
        //待发货
        [[_headView.waitBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            CAOrderListViewController *orderListVC = [CAOrderListViewController new];
            orderListVC.status = @"2";
            orderListVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:orderListVC animated:YES];
        }];
        //待收货
        [[_headView.collectBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            CAOrderListViewController *orderListVC = [CAOrderListViewController new];
            orderListVC.status = @"3";
            orderListVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:orderListVC animated:YES];
        }];
        //待收货
        [[_headView.afterBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            CAReturnGoodsListViewController *returnGoodsVC = [CAReturnGoodsListViewController new];
            returnGoodsVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:returnGoodsVC animated:YES];
        }];
    }
    return _headView;
}

- (UITableView *) mineTableView {
    if (!_mineTableView) {
        _mineTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [_mineTableView setBackgroundColor:RGBCOLOR(241, 241, 241)];
        _mineTableView.tableHeaderView = self.headView;
        _mineTableView.dataSource = self;
        _mineTableView.delegate = self;
    }
    return _mineTableView;
}

#pragma mark private method
- (void) getInfomation {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    CAUserInfomationApi *infomationApi = [[CAUserInfomationApi alloc] init];
    [infomationApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        CAUserInfomationApi *api = (CAUserInfomationApi *) request;
        if (api.done) {
            NSLog(@"%@", api.responseObject);
            [GVUserDefaults standardUserDefaults].level = api.responseObject[@"result"][@"level"];
            [GVUserDefaults standardUserDefaults].levelName = api.responseObject[@"result"][@"levelName"];
            [GVUserDefaults standardUserDefaults].mobile = api.responseObject[@"result"][@"mobile"];
            [GVUserDefaults standardUserDefaults].inviteCode = api.responseObject[@"result"][@"invite_code"];
            [GVUserDefaults standardUserDefaults].portrait = api.responseObject[@"result"][@"portrait"];
            [GVUserDefaults standardUserDefaults].parent = api.responseObject[@"result"][@"parent"];
            [GVUserDefaults standardUserDefaults].parentName = api.responseObject[@"result"][@"parent_name"];
            [GVUserDefaults standardUserDefaults].money = [NSString stringWithFormat:@"%ld", [api.responseObject[@"result"][@"money"] integerValue]];
            [GVUserDefaults standardUserDefaults].name = api.responseObject[@"result"][@"name"];
            [GVUserDefaults standardUserDefaults].cidName = api.responseObject[@"result"][@"cid_name"];
            [GVUserDefaults standardUserDefaults].cidAddress = api.responseObject[@"result"][@"cid_address"];
            [GVUserDefaults standardUserDefaults].cid = api.responseObject[@"result"][@"cid"];
            [GVUserDefaults standardUserDefaults].wechat = api.responseObject[@"result"][@"wechat"];
            [GVUserDefaults standardUserDefaults].isLogin = YES;
            
            [self.headView.logoImageView sd_setImageWithURL:[NSURL URLWithString:GVUD.portrait]];
            self.headView.nameLabel.text = GVUD.name;
            [self.mineTableView reloadData];
        } else {
            [SVProgressHUD lm_displayErrorWithStatus:api.errorMsg];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [SVProgressHUD lm_displayErrorWithStatus:@"请求出错"];
    }];
    
}

#pragma mark UITableView DataSource
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger) section {
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *array = self.dataArray[section][@"rowData"];
    return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIndentifier];
        cell.textLabel.textColor = RGBCOLOR(102, 102, 102);
    }
    NSLog(@"section:%ld     row:%ld", indexPath.section, indexPath.row);
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    NSArray *data = [[self.dataArray objectAtIndex:indexPath.section] objectForKey:@"rowData"];
    cell.imageView.image = [UIImage imageNamed:[[data objectAtIndex:indexPath.row] objectForKey:@"image"]];
    cell.textLabel.text = [[data objectAtIndex:indexPath.row] objectForKey:@"text"];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            NSLog(@"%@", GVUD.money);
            if (GVUD.money != nil) {
                cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2f", [GVUD.money floatValue]];
            }
            cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
            cell.detailTextLabel.textColor = RGBCOLOR(241, 90, 36);
//            cell.detailTextLabel.text = [NSString stringWithFormat:@"￥%@", [GVUserDefaults standardUserDefaults].money];
        }
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSLog(@"section:%ld indexpath:%ld", indexPath.section, indexPath.row);
        if (indexPath.section == 0) {
            if (indexPath.row == 0) {
                if ([GVUD.token isEqualToString:@""] || GVUD.token == nil) {
                    CALoginViewController *loginVC = [CALoginViewController new];
                    loginVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:loginVC animated:YES];
                } else {
                    CAMoneyViewController *moneyVC = [CAMoneyViewController new];
                    moneyVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:moneyVC animated:YES];
                }
            } else if (indexPath.row == 1) {
                if ([GVUD.token isEqualToString:@""] || GVUD.token == nil) {
                    CALoginViewController *loginVC = [CALoginViewController new];
                    loginVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:loginVC animated:YES];
                } else {
                    CAMyQRCodeViewController *qrCodeVC = [CAMyQRCodeViewController new];
                    qrCodeVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:qrCodeVC animated:YES];
                }
            } else if (indexPath.row == 2) {
                if ([GVUD.token isEqualToString:@""] || GVUD.token == nil) {
                    CALoginViewController *loginVC = [CALoginViewController new];
                    loginVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:loginVC animated:YES];
                } else {
                    CAHBXXViewController * hbVC = [CAHBXXViewController new];
                    hbVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:hbVC animated:YES];
                }
            } else if (indexPath.row == 3) {
                if ([GVUD.token isEqualToString:@""] || GVUD.token == nil) {
                    CALoginViewController *loginVC = [CALoginViewController new];
                    loginVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:loginVC animated:YES];
                } else {
                    CAHBJLViewController *hbjlVC = [CAHBJLViewController new];
                    hbjlVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:hbjlVC animated:YES];
                }
            } else if (indexPath.row == 4) {
                    if ([GVUD.token isEqualToString:@""] || GVUD.token == nil) {
                    CALoginViewController *loginVC = [CALoginViewController new];
                    loginVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:loginVC animated:YES];
                } else {
                    CAMembersSearchViewController *searchVC = [CAMembersSearchViewController new];
                    searchVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:searchVC animated:YES];
                }
            } else if (indexPath.row == 5) {
                    if ([GVUD.token isEqualToString:@""] || GVUD.token == nil) {
                    CALoginViewController *loginVC = [CALoginViewController new];
                    loginVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:loginVC animated:YES];
                } else {
                    CAMembersViewController *membersVC = [CAMembersViewController new];
                    membersVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:membersVC animated:YES];
                }
            }
        } else if (indexPath.section == 1) {
            if (indexPath.row ==0) {
                
            } else if (indexPath.row == 1) {
                    if ([GVUD.token isEqualToString:@""] || GVUD.token == nil) {
                    CALoginViewController *loginVC = [CALoginViewController new];
                    loginVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:loginVC animated:YES];
                } else {
                    CAAddressViewController *addressVC = [CAAddressViewController new];
                    addressVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:addressVC animated:YES];
                }
            } else if (indexPath.row == 2) {
                    if ([GVUD.token isEqualToString:@""] || GVUD.token == nil) {
                        CALoginViewController *loginVC = [CALoginViewController new];
                        loginVC.hidesBottomBarWhenPushed = YES;
                        [self.navigationController pushViewController:loginVC animated:YES];
                    } else {
                        CABankCardViewController *bankCardVC = [CABankCardViewController new];
                        bankCardVC.hidesBottomBarWhenPushed = YES;
                        [self.navigationController pushViewController:bankCardVC animated:YES];
                    }
            } else if (indexPath.row == 5) {
                [GVUD clearUserDefaults];
                [self getInfomation];
            }
        }
//    } else {
//        CALoginViewController *loginVC = [CALoginViewController new];
//        loginVC.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:loginVC animated:YES];
//    }
    
}

@end
